#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *

class Head:
  
  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)


  def getHeadTags(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_config_where_id % id) == 0):
      sql=Glob.select_all_from_config_where_id % id
      logging.debug(u'Head: getHeadTags() SQL error : %s' % sql)
    else:
      result=self.mDB.resultSQL()
      for row in result:
        self.title=row[1]
        self.pub=row[2]
        self.description=row[3]
        self.keyword=row[4]
        self.headtag=row[5]
        self.url=row[6]
        self.email=row[7]
        self.skin=row[8]
        self.language=row[9]
        self.name=row[10]
        
        html_head="""
    <title>%s</title>
    <base href="%s" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta http-equiv="content-script-type" content="text/javascript" /> 
    <meta http-equiv="content-style-type" content="text/css" /> 
    <meta http-equiv="expires" content="0" /> 
    <meta http-equiv="pragma" content="no-cache" />
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />    
    <meta name="resource-type" content="document" /> 
    <meta name="distribution" content="global" /> 
    <meta name="author" content="JPhG" /> 
    <meta name="copyright" content="Copyright (c) by JPhG for Comics CMS" /> 
    <meta name="owner" content="http://lugocreation@gmail.com" /> 
    <meta name="language" content="%s" /> 
    <meta http-equiv="content-language" content="en-en, fr, fr-be, fr-ca, fr-lu, fr-ch" /> 
    <meta name="Publisher" content="JPhG" /> 
    <meta name="Date-Creation-yyyymmdd" content="20100511" />
    <meta name="keywords" content="%s" />
    <meta name="description" content="%s" />
    <link rel="stylesheet" type="text/css" href="skin/%s/css/stylesheet.css" />
    <link rel="apple-touch-icon" href="skin/%s/img/apple-touch-icon.png" />     
    <script src="js/comics.js" type="text/javascript"></script>
    <script src="js/tiny_mce/tiny_mce.js" type="text/javascript"></script>
    %s
                  """ % (self.title, self.url, self.language, self.keyword, self.description, self.skin, self.skin, self.headtag)
    
    if html_head:
      return html_head
    else:
      return 0  


  def getConfigListID(self):
    if (self.mDB.executSQL(Glob.select_id_from_config) == 0):
      logging.debug(u'Head: getConfigListID() SQL error : %s' % select_id_from_config)
    else:
      result=self.mDB.resultSQL()
      lst=[]
      for row in result:
        lst.append(row[0])
      return lst


  def getHtmlSelectListID(self, selected):
    listID=self.getConfigListID()
    html_select = "<select id='config_id' onchange='modifyConfig()'>\n"
    for id in listID:
      if id == int(selected):
        html_select += "<option value='%s' selected>Configuration %s</option>\n" % (id, id)
      else:
        html_select += "<option value='%s'>Configuration %s</option>\n" % (id, id)
    html_select += "</select>\n"
    return html_select


  def getConfig(self):
    if self.title:
      return { u'title': self.title, u'pub': self.pub, u'description': self.description, u'keyword': self.keyword, u'headtag': self.headtag, u'url': self.url, u'email': self.email, u'skin': self.skin, u'language': self.language, u'name': self.name }
    else:
      return -1


  def setConfig(self, id, title, pub, description, keyword, headtag, url, email, skin, language, name):
    u_title=unicode(title.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_pub=unicode(pub.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_description=unicode(description.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_keyword=unicode(keyword.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_headtag=unicode(headtag.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_url=unicode(url, u'utf8')
    u_email=unicode(email, u'utf8')
    u_skin=unicode(skin, u'utf8')
    u_language=unicode(language, u'utf8')
    u_name=unicode(name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    
    if (self.mDB.executSQL(Glob.update_config_where_id % (u_title, u_pub, u_description, u_keyword, u_headtag, u_url, u_email, u_skin, u_language, u_name, id) ) == 0):
      sql=Glob.update_config_where_id % (u_title, u_pub, u_description, u_keyword, u_headtag, u_url, u_email, u_skin, u_language, u_name, id)
      logging.debug(u'Head: setConfig() SQL error : %s' % sql)
      return 0
    else:
      return 1    


  def newConfig(self, title, pub, description, keyword, headtag, url, email, skin, language, name):
    u_title=unicode(title.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_pub=unicode(pub.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_description=unicode(description.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_keyword=unicode(keyword.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_headtag=unicode(headtag.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_url=unicode(url, u'utf8')
    u_email=unicode(email, u'utf8')
    u_skin=unicode(skin, u'utf8')
    u_language=unicode(language, u'utf8')
    u_name=unicode(name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    
    if (self.mDB.executSQL(Glob.insert_config % (u_title, u_pub, u_description, u_keyword, u_headtag, u_url, u_email, u_skin, u_language, u_name) ) == 0):
      sql=Glob.insert_config % (u_title, u_pub, u_description, u_keyword, u_headtag, u_url, u_email, u_skin, u_language, u_name)
      logging.debug(u'Head: newConfig() SQL error : %s' % sql)
      return 0
    else: 
      if (self.mDB.executSQL(Glob.last_insert_id_config ) == 0):
        logging.debug(u'Head: newConfig() SQL error : %s' % Glob.last_insert_id_config)
        return 0
      else:
        result=self.mDB.resultSQL()
        for row in result:
          id=row[0]
        if id:
         return id 


  def deleteConfig(self, id):
    if (self.mDB.executSQL(Glob.delete_config_where_id % id) == 0):
      sql=Glob.delete_config_where_id % id
      logging.debug(u'Head: deleteConfig() SQL error : %s' % sql)
      return 0
    else:
      return 1
